<?php

namespace services\models;

class warehouse {

    private $_db;
    private $_cache;

    public function __construct($db, $cache) {
        $this->_db = $db;
        $this->_cache = $cache;
    }

    public function get_stock($sku) {
        if (isset($sku)) {
            $result = $this->_db->queryFirst("
                    SELECT COUNT(p.sku) as STOCK 
                    FROM oms_live.wms_inventory i 
                        Inner Join oms_live.ims_product p On i.fk_product = p.id_product 
                    WHERE   
                        i.fk_inventory_status = 1 
                        AND (p.sku = :sku)
                    GROUP BY p.sku
                  ", array(':sku' => "$sku"));

            $exce = $this->_db->getPDOException();
            if ($exce) {
                throw new $exce;
            }
            return $result;
        }
    }

    public function get_status_history_by_order_item($id_sales_order) {
        
        if (isset($id_sales_order)) {
            $results = $this->_db->query("                    
                SELECT *
                FROM ims_sales_order_item_status_history h 
                INNER JOIN ims_sales_order_item_status so ON h.fk_sales_order_item_status = so.id_ims_sales_order_item_status
                WHERE h.fk_sales_order_item = :id_sales_order
                ", array(':id_sales_order' => "$id_sales_order"));

            $exce = $this->_db->getPDOException();
            if ($exce) {
                throw new $exce;
            }
            return $results;
        }
    }

    public function get_shipment_provider($id_sales_order) {
         if (isset($id_sales_order)) {
            $results = $this->_db->queryFirst("                    
                SELECT sp.id_shipment_provider,sp.shipment_provider_name,ps.`status`
                FROM ims_sales_order s 
                INNER JOIN oms_package p ON s.id_sales_order = p.fk_sales_order
                INNER JOIN oms_package_dispatching pd ON p.id_package = pd.fk_package
                INNER JOIN oms_package_status ps ON pd.id_package_dispatching = ps.fk_package_dispatching
                INNER JOIN oms_shipment_provider sp ON pd.fk_shipment_provider = sp.id_shipment_provider
                WHERE s.id_sales_order = :id_sales_order
                ", array(':id_sales_order' => "$id_sales_order"));

            $exce = $this->_db->getPDOException();
            if ($exce) {
                throw new $exce;
            }
            return $results;
        }
    }

}

?>
